import { querySql } from '../db/mysql';
import { validationResult } from 'express-validator';
import boom from '@hapi/boom';

import { CODE_ERROR, CODE_SUCCESS } from '../utils/constant';

export function getTaskList(req, res, next) {

    const err  = validationResult(req);

    if(!err.isEmpty()){
        const errList = err.array({onlyFirstError: true})
        next(boom.badRequest(errList[0].msg))   
        return
    }

    const {miniprogramType} = req.params
    const { pageNo, limit} = req.query
    const startPage = (pageNo-1) * limit
    const queryStr = `select * from task where type='${miniprogramType}' limit ${startPage}, ${limit}`;
    querySql(queryStr).then(data => {
        // console.log('data', data)
        res.json({
            code: CODE_SUCCESS,
            msg: '成功',
            data
        })
    })
}